var express = require('express');
var router = express.Router();
var fs = require('fs')
var path = require('path')
var Promise = require('bluebird')

var userCtrl = require('../controllers/userController')
var adminCtrl = require('../controllers/adminController')
var pageCtrl = require('../controllers/pageController')
var testCtrl = require('../controllers/testController')
var activeCtrl = require('../controllers/activeController')
var postCtrl = require('../controllers/postController')
var commentCtrl = require('../controllers/commentController')

//  管理员认证接口
router.use(function(req, res, next) {
    var route = req.path.toLowerCase()
    if (route.indexOf('admin') > -1 && route.indexOf('login') == -1 && !req.session.adminId) {
        res.redirect('/admin/loginPage')
    } else {
        next()
    }
})

//  验证码认证接口
router.use(function(req, res, next) {
    if (req.body.verifyCode) {
        if (req.session.verifyCode != req.body.verifyCode) {
            return res.json({
                code: 122,
                msg: '验证码错误'
            })
        }
        if (req.session.bindEmail != req.body.user.email) {
            return res.json({
                code: 122,
                msg: '邮箱已更换，请重新发送验证码'
            })
        }
        if (req.body.isChangeBindEmail && req.body.password != req.body.user.password) {
            return res.json({
                code: 122,
                msg: '密码错误'
            })
        }
        next()
    } else {
        next()
    }
})

//------------------后台页面渲染接口-----------------------//
router.get('/admin', adminCtrl.adminHomePage)
router.get('/admin/testPage', adminCtrl.adminTestPage)
router.get('/admin/postPage', adminCtrl.adminPostPage)
router.get('/admin/sitePage', adminCtrl.adminSitePage)
router.get('/admin/userPage', adminCtrl.adminUserPage)
router.get('/admin/loginPage', adminCtrl.adminLoginPage)


//------------------用户注册、登陆、个人中心接口--------------//

router.get('/user/list', userCtrl.getAll)

router.get('/user/login', userCtrl.login)
router.post('/user/register', userCtrl.register)

router.get('/user/baseInfo/id/:id', userCtrl.getUserInfo)
router.put('/user/baseInfo/id/:id', userCtrl.updateUserInfo)

router.put('/user/password/id/:id', userCtrl.changePassword)

router.get('/user/verifyCode', userCtrl.getVerifyCode)

//  申请管理员
router.post('/user/applyManage', userCtrl.applyManage);
//  投诉建议
router.post('/user/suggest', userCtrl.suggest)


//-----------------后台页面接口位置----------------//

//  管理员登陆
router.get('/admin/login', adminCtrl.login)

router.get('/admin/adminer', adminCtrl.getAll)
router.get('/admin/apply', adminCtrl.getApply)

router.put('/admin/apply/id/:id', adminCtrl.updateApply)

router.delete('/admin/user/id/:id', adminCtrl.removeUser)


//-----------------习题接口位置----------------//
//  获取习题记录总接口   type：获取类型（topic，practice，result）， page： 页码
router.get('/test/type/:type/page/:page', testCtrl.getTestList);
//  创建新习题总接口
router.post('/test/newTest/type/:type', testCtrl.create);
router.put('/test/updateTest/type/:type', testCtrl.update)
    //  获取单个习题或答案内容
router.get('/test/type/:type/id/:id', testCtrl.getOneById)
    //  删除单个习题后答案数据
router.delete('/test/type/:type/id/:id', testCtrl.remove)

//-----------------最新动态----------------//
router.get('/active', activeCtrl.getActiveList)

//-----------------帖子接口位置----------------//
//  获取帖子列表
router.get('/post/page/:page', postCtrl.getPostList)

//  获取单个帖子信息
router.get('/post/id/:id', postCtrl.getOne)
router.delete('/post/id/:id', postCtrl.remove)

//  发表帖子
router.post('/post', postCtrl.add)

//  发表回复
router.post('/post/comment/id/:id', commentCtrl.add)

//  赞同或反对观点
router.post('/post/viewport/id/:id', postCtrl.addViewport)

//  收藏帖子
router.post('/post/collect/id/:id', postCtrl.addCollect)



module.exports = router;